using System;
using System.Collections.Generic;
using System.Linq;

namespace TemplateWeb.Models.Repositories
{
	#region using
    using TemplateWeb.Models;
    using TemplateWeb.Helpers;
	#endregion
    public class DonDatHangRepository
	{
		public List<DonDatHang> SelectAll()
		{
			try
			{
				return DataContext.Instance.DonDatHang.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return new List<DonDatHang>();
		}

		public DonDatHang SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.DonDatHang.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(DonDatHang obj)
		{
			try
			{
				DataContext.Instance.DonDatHang.AddObject(obj);
				DataContext.Instance.SaveChanges();
                return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return false;// Cannot insert
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.DonDatHang.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.DonDatHang.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<DonDatHang> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.DonDatHang where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return new List<DonDatHang>();
		}

		public List<DonDatHang> SelectByIDUsers(Guid iduser)
		{
			try
			{
                var lstItem = (from item in DataContext.Instance.DonDatHang where item.IDUser == iduser select item).OrderByDescending(n => n.NgayDatHang).ToList();
				return lstItem;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			return new List<DonDatHang>();
		}
	}
}